package org.testis.gui.mvc.utilities;

import org.testis.utils.tree.MyDefaultMutableTreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class Operation {
    private MyDefaultMutableTreeNode m_source;
    private MyDefaultMutableTreeNode m_destination;
    private ComparisonTreeNode m_addToNode;
    private ComparisonTreeNode m_removeFromNode;
    private String m_operation;
    public static String COPY = "COPY";
    public static String CLONE = "CLONE";
    public static String REPLACE = "REPLACE";
    private OperationExecutor m_executor=null;
    private boolean m_replace=false;

    public Operation(String operation, ComparisonTreeNode addToNode, ComparisonTreeNode removeFromNode, OperationExecutor executor) {
        setSource(addToNode.getGuiReference());
        setDestination(removeFromNode.getGuiReference());
        setOperation(operation);
        setAddToNode(addToNode);
        setRemoveFromNode(removeFromNode);
        setExecutor(executor);
    }

    public MyDefaultMutableTreeNode getSource()
    {
        return m_source;
    }


    public void setSource(MyDefaultMutableTreeNode source)
    {
        m_source = source;
    }


    public MyDefaultMutableTreeNode getDestination()
    {
        return m_destination;
    }


    public void setDestination(MyDefaultMutableTreeNode destination)
    {
        m_destination = destination;
    }


    public String getOperation()
    {
        return m_operation;
    }


    public void setOperation(String operation)
    {
        if (operation!=null && operation.equals(REPLACE)) {
            System.out.println("REPLACE IS TRUE");
            setReplace(true);
        }
        m_operation = operation;
    }
    
    private void setReplace(boolean replace) {
        m_replace = replace;
    }
    
    public boolean isReplace() {
        return m_replace;
    }


    public ComparisonTreeNode getAddToNode()
    {
        return m_addToNode;
    }


    public void setAddToNode(ComparisonTreeNode addToNode)
    {
        m_addToNode = addToNode;
    }


    public ComparisonTreeNode getRemoveFromNode()
    {
        return m_removeFromNode;
    }


    public void setRemoveFromNode(ComparisonTreeNode removeFromNode)
    {
        m_removeFromNode = removeFromNode;
    }


    @Override
    public String toString()
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append(getOperation());
        buffer.append(" OF \n");
        buffer.append(m_source.getUserObject());
        return buffer.toString();
    }


    public Operation clone(String operation)
    {
        return new Operation(operation,m_addToNode,m_removeFromNode,m_executor);
    }


    public OperationExecutor getExecutor()
    {
        return m_executor;
    }


    public void setExecutor(OperationExecutor executor)
    {
        m_executor = executor;
    }
    
    public boolean execute() {
        return getExecutor().executeOperation(this);
    }
    
}
